import { ConfigService } from '@nestjs/config';

export interface RedisModuleOptions {
  host: string;
  port: number;
  password?: string;
  db: number;
  retryDelayOnFailover?: number;
  enableReadyCheck?: boolean;
  maxRetriesPerRequest?: number;
  lazyConnect?: boolean;
}

export const getRedisConfig = (configService: ConfigService): RedisModuleOptions => ({
  host: configService.get('REDIS_HOST', 'localhost'),
  port: configService.get('REDIS_PORT', 6379),
  password: configService.get('REDIS_PASSWORD'),
  db: configService.get('REDIS_DB', 0),
  retryDelayOnFailover: 100,
  enableReadyCheck: false,
  maxRetriesPerRequest: 3,
  lazyConnect: true,
});

export const getRedisUrl = (configService: ConfigService): string => {
  const host = configService.get('REDIS_HOST', 'localhost');
  const port = configService.get('REDIS_PORT', 6379);
  const password = configService.get('REDIS_PASSWORD');
  const db = configService.get('REDIS_DB', 0);
  
  if (password) {
    return `redis://:${password}@${host}:${port}/${db}`;
  }
  
  return `redis://${host}:${port}/${db}`;
};
